package com.ruoyi.provider.service.impl;

import com.ruoyi.provider.domain.AhctDefineDamage;
import com.ruoyi.provider.domain.AhctDefineDamageProject;
import com.ruoyi.provider.service.AhctDefineDamageProjectService;
import com.ruoyi.provider.service.ReportService;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author SunNan
 * @version 1.0
 * @date 2024/5/29 17:30
 * @description
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Resource
    private AhctDefineDamageProjectService ahctDefineDamageProjectService;

//    @Override
//    public void exportBusinessData(HttpServletResponse response, int flag, String case_number) {
//
//    }

    @Override
    public void excelDerive(HttpServletResponse response, int flag, String case_number) {

        //拿到案件基本信息
        AhctDefineDamage ahctDefineDamage = ahctDefineDamageProjectService.selectByCaseNumber(case_number);
            try {
                XLSTransformer transformer = new XLSTransformer();

                InputStream is = null;
                if (flag == 1){
                  //基于提供好的模板文件创建一个新的Excel表格对象
                    is = new FileInputStream("ruoyi-admin/src/main/resources/template/ahctQuotationList(seal).xlsx");
                }else{
                  //基于提供好的模板文件创建一个新的Excel表格对象
                    is = new FileInputStream("ruoyi-admin/src/main/resources/template/ahctQuotationList(noSeal).xlsx");
                }

                Map<String, Object> data = new HashMap();
                data.put("case_number", ahctDefineDamage.getCase_number());
                data.put("insurer", ahctDefineDamage.getInsurer());
                data.put("car_number", ahctDefineDamage.getCar_number());
                data.put("accident_address", ahctDefineDamage.getAccident_address());
//                data.put("damage_customer_phone", ahctDefineDamage.getDamage_customer_phone());
                data.put("update_time", ahctDefineDamage.getUpdate_time());
//                data.put("damage_customer_name", ahctDefineDamage.getDamage_customer_name());
                data.put("tax_rate", ahctDefineDamage.getTax_rate()/100.00);
                data.put("total_amount", ahctDefineDamage.getTotal_amount());
                data.put("opening_bank", ahctDefineDamage.getOpening_bank());
                data.put("bank_account_number", ahctDefineDamage.getBank_account_number());
                data.put("remarks", ahctDefineDamage.getRemarks());
                data.put("construction_amount", ahctDefineDamage.getConstruction_amount());
                data.put("taxes", ahctDefineDamage.getTaxes());

                //集合数据
                data.put("list",this.itemDetail(case_number));

                Workbook wk = transformer.transformXLS(is, data);
                is.close();
                //通过输出流将文件下载到客户端浏览器中
                ServletOutputStream out = response.getOutputStream();
                wk.write(out);
                //清空缓存和关闭资源
                out.flush();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
            }

    }

    public List<AhctDefineDamageProject> itemDetail(String case_number){

        //获取案件对应的物损信息列表
        List<AhctDefineDamageProject> damageProperty = ahctDefineDamageProjectService.getDamageProperty(case_number);
        int index = 1;
        List<AhctDefineDamageProject> list = new ArrayList<>();
        for (AhctDefineDamageProject da : damageProperty){
            AhctDefineDamageProject ahctDefineDamageProject = new AhctDefineDamageProject();
            da.setIndex(index++);//序号
            ahctDefineDamageProject.setIndex(da.getIndex());
            ahctDefineDamageProject.setDamage_project_name(da.getDamage_project_name());
            ahctDefineDamageProject.setDamage_project_price(da.getDamage_project_price());
            ahctDefineDamageProject.setDamage_project_number(da.getDamage_project_number());
            ahctDefineDamageProject.setUnit(da.getUnit());
            ahctDefineDamageProject.setRemarks(da.getRemarks());
            list.add(ahctDefineDamageProject);
        }
        return list;
    }
}
